#include "servewidget.h"
#include "ui_servewidget.h"
#include <QString>
#include <QDebug>
#include <QMap>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

ServeWidget::ServeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ServeWidget)
{
    ui->setupUi(this);

    //初始化套接字
    tcpServer = NULL;
    tcpSocket = NULL;

    tcpServer = new QTcpServer(this);

    //设定端口
    tcpServer->listen(QHostAddress::Any,8888);

    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(showInfoOfConnect()));
}

void ServeWidget::showInfoOfConnect() {

    tcpSocket = tcpServer->nextPendingConnection();
    QString ip = tcpSocket->peerAddress().toString();
    qint16 port = tcpSocket->peerPort();
    QString temp = QString("有设备成功接入，设备ip：%1").arg(ip);
    ui->label->setText(temp);
    qDebug() << temp;

    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(getMessage()));
}

void ServeWidget::getMessage(){
    //从套接字取出内容
    QByteArray array = tcpSocket->readAll();
    QString str = array;
    QString temp = QString("客户端发来的信息：%1").arg(str);
    qDebug() << temp;
    ui->label_2->setText(temp);
    if(str == "TodayTemperature"){
//       QString text = "21-29'C\n 阴转多云\n 无持续风向微风\n";
//       tcpSocket->write(text.toUtf8().data());
         QJsonObject subjson;
         subjson.insert("dayName","周六");
         subjson.insert("time","10月26日");
         subjson.insert("temperture","21-29'C");
         subjson.insert("weather","阴");
         subjson.insert("wind","无持续风向微风");
         subjson.insert("quality","优");

         QJsonDocument doc;
         doc.setObject(subjson);
         //doc.setArray(jsonArr);
         QByteArray data = doc.toJson(QJsonDocument::Compact);
         qDebug() << data;
         tcpSocket->write(data);

    }
    else if (str == "allData"){
        //json对象
        QJsonObject subjson[5];

        subjson[0].insert("dayName","周六");
        subjson[0].insert("time","10月26日");
        subjson[0].insert("temperture","21-29'C");
        subjson[0].insert("weather","阴");
        subjson[0].insert("wind","无持续风向微风");
        subjson[0].insert("quality","优");

        subjson[1].insert("dayName","周日");
        subjson[1].insert("time","10月27日");
        subjson[1].insert("temperture","22-28'C");
        subjson[1].insert("weather","阴转多云");
        subjson[1].insert("wind","无持续风向微风");
        subjson[1].insert("quality","优");

        subjson[2].insert("dayName","周一");
        subjson[2].insert("time","10月28日");
        subjson[2].insert("temperture","21-30'C");
        subjson[2].insert("weather","阴转小雨");
        subjson[2].insert("wind","无持续风向微风");
        subjson[2].insert("quality","优");

        subjson[3].insert("dayName","周二");
        subjson[3].insert("time","10月29日");
        subjson[3].insert("temperture","20-28'C");
        subjson[3].insert("weather","阴转多云");
        subjson[3].insert("wind","无持续风向微风");
        subjson[3].insert("quality","优");

        subjson[4].insert("dayName","周三");
        subjson[4].insert("time","10月30日");
        subjson[4].insert("temperture","21-29'C");
        subjson[4].insert("weather","阴");
        subjson[4].insert("wind","无持续风向微风");
        subjson[4].insert("quality","优");

        //json数组
        QJsonArray jsonArr;
        for(int i=0; i<5;i++){
            jsonArr.append(QJsonValue(subjson[i]));
        }

        //ChartData
        QJsonObject subjson1[5];
        subjson1[0].insert("time","11");
        subjson1[0].insert("wendu","23");
        subjson1[1].insert("time","14");
        subjson1[1].insert("wendu","28");
        subjson1[2].insert("time","17");
        subjson1[2].insert("wendu","24");
        subjson1[3].insert("time","20");
        subjson1[3].insert("wendu","21");
        subjson1[4].insert("time","23");
        subjson1[4].insert("wendu","20");
        //json数组
        QJsonArray jsonArr1;
        for(int i=0; i<5;i++){
            jsonArr1.append(QJsonValue(subjson1[i]));
        }

        QJsonObject json1;
        json1.insert("weatherData",jsonArr);
        json1.insert("chartData",jsonArr1);

        QJsonDocument doc;
        doc.setObject(json1);
        //doc.setArray(jsonArr);
        QByteArray data = doc.toJson(QJsonDocument::Compact);
        qDebug() << "返回数据" <<data;
        tcpSocket->write(data);
    }

}

//void ServeWidget::getMessage(){
//    //从套接字取出内容
//    QByteArray array = tcpSocket->readAll();
//    QString str = array;
//    QString temp = QString("客户端发来的信息：%1").arg(str);
//    qDebug() << temp;
//    ui->label_2->setText(temp);
//    QStringList list = str.split(",");
//    qDebug() << list[0];

//    QString str1;
//    for (int i=0; i<list.length(); i++) {
//        //根据参数给客户端返回数据
//        str1 = list[i];
//        qDebug() << str1;
//        if(str1 == "TodayTemperature"){
//    //       QString text = "21-29'C\n 阴转多云\n 无持续风向微风\n";
//    //       tcpSocket->write(text.toUtf8().data());
//             QJsonObject subjson;
//             subjson.insert("dayName","周六");
//             subjson.insert("time","10月26日");
//             subjson.insert("temperture","21-29'C");
//             subjson.insert("weather","阴");
//             subjson.insert("wind","无持续风向微风");
//             subjson.insert("quality","优");

//             QJsonDocument doc;
//             doc.setObject(subjson);
//             //doc.setArray(jsonArr);
//             QByteArray data = doc.toJson(QJsonDocument::Compact);
//             qDebug() << data;
//             tcpSocket->write(data);

//        }
//        else if (str1 == "DetialList"){
//            //json对象
//            QJsonObject subjson[5];

//            subjson[0].insert("dayName","周六");
//            subjson[0].insert("time","10月26日");
//            subjson[0].insert("temperture","21-29'C");
//            subjson[0].insert("weather","阴");
//            subjson[0].insert("wind","无持续风向微风");
//            subjson[0].insert("quality","优");

//            subjson[1].insert("dayName","周日");
//            subjson[1].insert("time","10月27日");
//            subjson[1].insert("temperture","22-28'C");
//            subjson[1].insert("weather","阴转多云");
//            subjson[1].insert("wind","无持续风向微风");
//            subjson[1].insert("quality","优");

//            subjson[2].insert("dayName","周一");
//            subjson[2].insert("time","10月28日");
//            subjson[2].insert("temperture","21-30'C");
//            subjson[2].insert("weather","阴转小雨");
//            subjson[2].insert("wind","无持续风向微风");
//            subjson[2].insert("quality","优");

//            subjson[3].insert("dayName","周二");
//            subjson[3].insert("time","10月29日");
//            subjson[3].insert("temperture","20-28'C");
//            subjson[3].insert("weather","阴转多云");
//            subjson[3].insert("wind","无持续风向微风");
//            subjson[3].insert("quality","优");

//            subjson[4].insert("dayName","周三");
//            subjson[4].insert("time","10月30日");
//            subjson[4].insert("temperture","21-29'C");
//            subjson[4].insert("weather","阴");
//            subjson[4].insert("wind","无持续风向微风");
//            subjson[4].insert("quality","优");

//            //json数组
//            QJsonArray jsonArr;
//            for(int i=0; i<5;i++){
//                jsonArr.append(QJsonValue(subjson[i]));
//            }

//            QJsonDocument doc;
//            //doc.setObject(json);
//            doc.setArray(jsonArr);
//            QByteArray data = doc.toJson(QJsonDocument::Compact);
//            qDebug() << data;
//            tcpSocket->write(data);
//        }
//        else if(str1 == "chartData"){
//            //json对象
//            QJsonObject subjson[5];
//            subjson[0].insert("time","11点");
//            subjson[0].insert("wendu","23");
//            subjson[1].insert("time","14点");
//            subjson[1].insert("wendu","28");
//            subjson[2].insert("time","17点");
//            subjson[2].insert("wendu","24");
//            subjson[3].insert("time","20点");
//            subjson[3].insert("wendu","21");
//            subjson[4].insert("time","23点");
//            subjson[4].insert("wendu","20");


//            //json数组
//            QJsonArray jsonArr;
//            for(int i=0; i<5;i++){
//                jsonArr.append(QJsonValue(subjson[i]));
//            }

//            //复杂json对象
//    //        QJsonObject json;
//    //        json.insert("data",jsonArr);

//            QJsonDocument doc;
//            //doc.setObject(json);
//            doc.setArray(jsonArr);
//            QByteArray data = doc.toJson(QJsonDocument::Compact);
//            qDebug() << data;
//            tcpSocket->write(data);
//        }
//    }

//}

ServeWidget::~ServeWidget()
{
    delete ui;
}
